<template>
  <div class="forgot-password-outer">
    <div class="forgot-password-card">
      <el-form :model="form" :rules="rules" ref="formRef" class="forgot-form">
        <el-form-item label="邮箱" prop="email">
          <div class="input-with-btn">
            <el-input v-model="form.email" placeholder="请输入邮箱" />
            <el-button
              type="primary"
              :disabled="countdown > 0 || !form.email"
              @click="sendCode"
              class="send-code-btn"
            >
              {{ countdown > 0 ? `${countdown}s后重试` : '获取验证码' }}
            </el-button>
          </div>
        </el-form-item>
        <el-form-item v-if="showCodeInput" label="验证码" prop="code">
          <el-input v-model="form.code" placeholder="请输入验证码" />
        </el-form-item>
        <el-form-item>
          <el-button
            type="success"
            :disabled="!form.code"
            @click="verifyCode"
            class="verify-btn"
          >验证</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup name="Forget">
import { sendEmailCode,validateEmailCode } from '@/api/login';
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
// import { sendCodeApi, verifyCodeApi } from '@/api/forgot-password'; // 你可以根据实际接口替换

const router = useRouter();
const formRef = ref();
const form = reactive({
  email: '',
  code: ''
});
const rules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email' as any, message: '邮箱格式不正确', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ]
};

const showCodeInput = ref(false);
const countdown = ref(0);
let timer: any = null;

const sendCode = async () => {
  if (!form.email) return;
  await sendEmailCode(form.email); // 调用发送验证码接口
  showCodeInput.value = true;
  countdown.value = 60;
  timer = setInterval(() => {
    countdown.value--;
    if (countdown.value <= 0) {
      clearInterval(timer);
      timer = null;
    }
  }, 1000);
};

const verifyCode = async () => {
  const res = await validateEmailCode(form.email, form.code);
  if (res.token) {
    router.push({ path: '/reset-password', query: { token: res.token ,email: form.email }} ); // 跳转到重置密码页面
  }
};
</script>

<style scoped>
.forgot-password-outer {
    margin-top: 20px;
  display: flex;
  justify-content: center;
  /* background: #f6f8fa; */
}

.forgot-password-card {
  max-width: 400px;
  width: 100%;
  padding: 32px 24px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 32px rgba(27, 31, 35, 0.07);
}
.forgot-form .el-form-item {
  display: flex;
  align-items: center;
}
.input-with-btn {
  display: flex;
  align-items: center;
  width: 100%;
}
.input-with-btn .el-input {
  flex: 1;
}
.send-code-btn {
  margin-left: 12px;
  white-space: nowrap;
}
.verify-btn {
  width: 100%;
}

@media (max-width: 600px) {
  .forgot-password-outer {
    display: flex;
    justify-content: center;
    margin-top: 20px;
  }
}
</style>
